#include "types.h"
#include "x86.h"

void* memset(void *dst, int c, uint n) {
	if ((int) dst % 4 == 0 && n % 4 == 0) {
		c &= 0xFF;
		stosl(dst, (c << 24) | (c << 16) | (c << 8) | c, n / 4);
	} else
		stosb(dst, c, n);
	return dst;
}

void* memmove(void *dst, const void *src, uint n) {
	const char *s;
	char *d;

	s = src;
	d = dst;
	if (s < d && s + n > d) {
		s += n;
		d += n;
		while (n-- > 0)
			*--d = *--s;
	} else
		while (n-- > 0)
			*d++ = *s++;

	return dst;
}

void* memcpy(void *dst, const void *src, uint n) {
	return memmove(dst, src, n);
}

int memcmp(const void *v1, const void *v2, uint n) {
	const uchar *s1, *s2;

	s1 = v1;
	s2 = v2;
	while (n-- > 0) {
		if (*s1 != *s2)
			return *s1 - *s2;
		s1++, s2++;
	}

	return 0;
}

char* strncpy(char *s, const char *t, int n) {
	char *os;

	os = s;
	while (n-- > 0 && (*s++ = *t++) != 0)
		;
	while (n-- > 0)
		*s++ = 0;
	return os;
}

//保证以0结尾
char* strncpyEnd0(char *s, const char *t, int n) {
	char *os;

	os = s;
	while (n-- > 0 && (*s++ = *t++) != 0)
		;
	while (n-- > 0)
		*s++ = 0;
	*(s+n-1) = 0;
	return os;
}

int strlen(const char *s) {
	int n;

	for (n = 0; s[n]; n++)
		;
	return n;
}

